package utilsys

import (
	"bytes"
	"log"
	"os/exec"
	"strings"
)

// IsProcessRunning chk process running state
func IsProcessRunning(proname string) bool {

	var stdout bytes.Buffer
	var stderr bytes.Buffer
	cmd := exec.Command("ps", "-ef")

	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()
	if err != nil {
		log.Fatal(err)
	}

	grep := exec.Command("grep", proname)
	grep.Stdin = strings.NewReader(stdout.String())
	var stdout2 bytes.Buffer
	grep.Stdout = &stdout2
	_ = grep.Run()
	bincount := strings.Count(stdout2.String(), "\n")
	return bincount > 1
}
